package king.dominic.jlibrary.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by king on 2017/4/5.
 * OriginRecyclerAdapter
 */
public abstract class OriginRecyclerAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
    protected OnItemClickListener onItemClickListener;

    protected void bindHolder(RecyclerView.ViewHolder holder,int position) {
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(onClickListener);
    }
    protected View.OnClickListener onClickListener=new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            if(onItemClickListener!=null)onItemClickListener.onClick(v, (Integer) v.getTag());
        }
    };
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;

    }

    @Override
    public void onBindViewHolder(VH holder, int position) {
        bindHolder(holder,position);
    }

    public interface OnItemClickListener {
        void onClick(View view, int position);
    }

    protected static View inflate(ViewGroup parent, int resId){
        return LayoutInflater.from(parent.getContext()).inflate(resId,parent,false);
    }
}
